// pages/servering/servering.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hours: '0' + 0, // 时
    minute: '0' + 0, // 分
    second: '0' + 0, // 秒
    time: "",
    newSecond: 0,
    newMinute: 0,
    newHour: 0,
    receiving: "",
    newTime: "",
    publishId: "",
    setInterval: "",
    feeOpen: false,
    feeItemIds: [],
    orderList: [
    ],
    projectMes:[]
  },
  grabOrder: function(options) {
    var that = this;
    if (options.id == null || options.id == undefined) {
      console.log(111)
      that.setData({
        publishId: wx.getStorageSync("orderId")
      });

    } else {
      that.setData({
        publishId: options.userId
      })
    }
    wx.showModal({
      title:"提示",
      content: '是否确认取消订单',
      confirmText: "确定",
      cancelText: "取消",
      success: function(res) {
        if (res.confirm) {
          wx.request({
            url: 'https://www.xiaozheng8.com/serviceOrder/setStatus/v2',
            method: "post",
            data: {
              "isCancel": true,
              publishId: that.data.publishId
            }
          })
          wx.reLaunch({
            url: '../order/order',
          });
          wx.showModal({
            title:"提示", 
            title: '订单已取消',
            showCancel: false
          })
        }
      }
    })
  },
  feeInfo: function() {
    this.setData({
      feeOpen: !this.data.feeOpen,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    console.log(options.userId);
    if (options.userId == null || options.userId == undefined) {
      that.setData({
        publishId: wx.getStorageSync("orderId")
      });
      console.log("本地储存传值")
    } else {
      that.setData({
        publishId: options.userId
      });
      console.log("orderId传值")
    }
    console.log(that.data.publishId + "这是publishId");
    wx.request({
      url: 'https://www.xiaozheng8.com/serviceOrder/sendFindById',
      method: "post",
      data: {
        publishId: that.data.publishId,

      },
      success: function(res) {
        console.log(res),
          console.log(that.data.publishId + "punlishId");
        that.setData({
          orderList: res.data.data,
          publishId: that.data.publishId,
          projectMes:res.data.data.project
        });
        console.log(that.data.orderList);
        that.setData({
          orderList: [that.data.orderList],
          time: res.data.data.createTime
        });
        var fee = res.data.data.feeItemIds;
        console.log(fee);
        that.data.feeItemIds.push(fee.split(" "));
        that.setData({
          feeItemIds: that.data.feeItemIds,
        })
        console.log(that.data.feeItemIds)
      }
    })
    console.log(that.data.publishId+"这是时间上的id")
    wx.request({
      url: 'https://www.xiaozheng8.com/clock/get',
      method: "post",
      data: {
        publishId: parseInt(that.data.publishId),
        userType: 0
      },
      success: function(res) {
        console.log(res)
        console.log(res.data.data);
        that.setData({
          newTime: res.data.data
        })
      
      }



    });

    console.log(options);


  },
  order:function(){
    var that=this;
    this.data.getData = setInterval(function () {    
      wx.request({
        url: 'https://www.xiaozheng8.com/serviceOrder/sendFindById',
        method: "post",
        data: {
          publishId: that.data.publishId,

        },
        success: function (res) {
          console.log(that.data.orderList);
          if (res.data.data.status >= 5) {
            wx.switchTab({
              url: '../userOrder/userOrder',
            }),
              console.log(that.data.publishId);
          }
        }
      })
    }, 10000)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    console.log(that.data.newTime + "newTime");
    this.time();
    this.order();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    clearInterval(this.data.setInterval);
    clearInterval(this.data.getData);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    var that = this;
    clearInterval(that.data.setInterval);
    clearInterval(this.data.getData);

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  time: function() {


    const that = this;

    var getTime = 0;
    console.log(that.data.publishId + "这是时间上的id2")
    wx.request({
      url: 'https://www.xiaozheng8.com/clock/get',
      method: "post",

      data: {
        publishId: parseInt(that.data.publishId) ,
        userType: 0
      },

      success: function(res) {
        console.log(res)
        console.log(res.data.data);
        that.setData({
          newTime: res.data.data
        })
        getTime = res.data.data;
        console.log(that.data.newTime + "时间")

        console.log(that.data.newTime + "计时器时间")

        var time = that.data.newTime;
        var second = time % 60;
        var minute = parseInt(time / 60);
        var hours = parseInt(parseInt(time / 60) / 60);
        that.setData({
          second: time % 60,
          minute: parseInt(time / 60),
          hours: parseInt((parseInt(time) / 60) / 60),
        });
        that.data.setInterval = setInterval(function() {        
          time++;
          if (time % 60 < 10) {
            that.setData({
              second: '0' + time % 60
            })
          } else {
            that.setData({
              second: time % 60
            })
          }
          if (parseInt(time / 60)%60 < 10) {
            that.setData({
              minute: '0' + parseInt(time / 60)%60
            })
          } 
          // else if (parseInt(time / 60)> 59){
          //   that.setData({
          //     minute: parseInt(time / 60)%60
          //   })
          
          // } 
          else {
            that.setData({
              minute: parseInt(time / 60)%60,

            })
          }
        }, 1000)
      }
    });
  },

})